﻿using System;
using System.Collections.Generic;
using Authentication.Data.Repository;
using Authentication.Model.Model; 
using Authentication.Model.Interface;
namespace Authentication.Provider
{
    public class ApplicationProvider : IApplicationProvider
    {
        public IEnumerable<Application> RetrieveApplications(string name = null)
        {
            if (string.IsNullOrEmpty(name))
                return AuthenticationRepository.Current.GetAll<Application>();
            else
                return AuthenticationRepository.Current.Find<Application>(x => x.ApplicationName.ToUpper().Contains(name.Trim().ToUpper()));
        }

        public Application RetrieveApplication(string applicationKey)
        {
            Guid key = Guid.Empty;
            if (Guid.TryParse(applicationKey, out key))
                return RetrieveApplication(key);
            else
                return AuthenticationRepository.Current.FindOne<Application>(x => x.ApplicationName.ToUpper() == applicationKey.Trim().ToUpper());
        }

        public Application RetrieveApplication(Guid applicationKey)
        {
            return AuthenticationRepository.Current.FindOne<Application>(x => x.ApplicationKey == applicationKey);
        }
    }
}
